#include "watchdog.h"

#include <pybind11/pybind11.h>

namespace py = pybind11;

PYBIND11_MODULE(locke_interface,m)
{
    m.doc() = "pybind11 locke interface plugin";

    py::class_<watchdog>(m,"watchdog")
        .def(py::init<>())
        .def("init",&watchdog::init,"init")
        .def("getWatchDogInfo",&watchdog::getWatchDogInfo,"getWatchDogInfo")
        .def("getWatchDogTime",&watchdog::getWatchDogTime,"getWatchDogTime")
        .def("feedDog",&watchdog::feedDog,"feedDog")
        ;
  
}



watchdog::watchdog(/* args */)
{

}

watchdog::~watchdog()
{
    printf("watchdog exit !!!~~~\n\r");
    disab();
    dev_close();
}

//初始化看门狗函数
int watchdog::init(std::string dev)
{
    int i,ret;
    //open device file
    fd = open(dev.c_str(),O_RDWR);   //打开看门狗设备
    if(fd < 0)
    {
        printf("device open %s fail\n",dev.c_str());
        return -1;
    }
    printf("open %s success\n",dev.c_str());

    return 0;
}

//获取看门狗信息
int watchdog::getWatchDogInfo(){

    //读板卡信息，但不常用
    ioctl(fd,WDIOC_GETSUPPORT,&wi);
    printf("options is %d,identity is %s\n",wi.options,wi.identity);

    return 0;
}

//获取看门狗超时时间
int watchdog::getWatchDogTime(){
    ioctl(fd,WDIOC_GETTIMEOUT,&time);
    printf("The WatchDog over time is: %d s.\n", time);
    return time;
}
//设置看门狗超时时间
int watchdog::setWatchDogTime(int time){
    int i,ret=0;

    disab();

    //设置超时时间
    ret = ioctl(fd,WDIOC_SETTIMEOUT,&time);

    enab();

    return ret;
}

int watchdog::disab(){
    int i=0;
    int ret;

    //关闭
    i=WDIOS_DISABLECARD;//WDIOC_SETOPTIONS=0X0001
    ret = ioctl(fd,WDIOC_SETOPTIONS,&i);

    return ret;
}
int watchdog::enab(){
    int i=0;
    int ret;

    //打开看门狗
    i=WDIOS_ENABLECARD;//WDIOS_ENABLECARD 0x0002
    ret = ioctl(fd,WDIOC_SETOPTIONS,&i);

    return ret;
}
int watchdog::feedDog(){

    ioctl(fd,WDIOC_KEEPALIVE,NULL);

    return 0;
}
int watchdog::dev_close(){
    close(fd);
    return 0;
}